package com.shxy.rlzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shxy.rlzy.entity.Menu;
import com.shxy.rlzy.entity.User;
import com.shxy.rlzy.mapper.MenuMapper;
import com.shxy.rlzy.mapper.UserMapper;
import com.shxy.rlzy.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanhao
 * @since 2021-08-16
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
    private MenuMapper menuMapper;  // 菜单管理
    private UserMapper userMapper;  // 用户管理

    @Autowired
    public void setMenuMapper(MenuMapper menuMapper) {
        this.menuMapper = menuMapper;
    }
    @Autowired
    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<Menu> getNavsByAdminId(Long userId) {
        // 获取菜单信息并过滤掉非菜单项
        List<Menu> menus = menuMapper.getUserMenus(userId).stream().filter(menu -> (menu.getType() == 0 || menu.getType() == 1)).toList();
        // 将菜单转换成树形结构后返回
        return buildTreeMenu(menus);
    }

    @Override
    public List<Menu> getNavsByUserName(String username) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        return buildTreeMenu(getNavsByAdminId(user.getId()));
    }

    @Override
    public List<Menu> getMenusAll() {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.ne("statu", "-1");
        qw.orderByAsc("orderNum");
        return buildTreeMenu(this.list(qw));
    }

    @Override
    public List<Menu> getMenusAndChildrenById(Long id) {
        return null;
    }

    @Override
    public boolean hasChild(Long id) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("parent_id", id);
        return count(qw) > 0;
    }

    @Override
    public List<Menu> getChildren(Long id) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("parent_id", id);
        List<Menu> menus = list(qw);
        menus.forEach(menu -> {
            menu.setChildren(getChildren(menu.getId()));
        });
        return menus;
    }

    @Override
    public List<Long> getChildrenIds(Long id) {
        // 1、创建列表
        List<Long> ids = new ArrayList<>(Arrays.asList(id));
        // 2、获取子节点
        List<Menu> menus = getChildren(id);
        // 3、递归获取子节点编号
        menus.forEach(menu -> {
            ids.addAll(getChildrenIds(menu.getId()));
        });
        return ids;
    }

    @Override
    public boolean deleteMenusByIds(List<Long> ids) {
        AtomicInteger c = new AtomicInteger();
        ids.forEach(id -> {
            if(deleteMenuById(id)) c.addAndGet(1);
        });
        return c.get() > 0;
    }

    @Override
    public boolean deleteMenuById(Long id) {
        return menuMapper.setStatuById(id, -1) > 0;
    }

    /**
     * 将列表转换成树
     * @param menus
     * @return
     */
    private List<Menu> buildTreeMenu(List<Menu> menus) {

        List<Menu> finalMenus = new ArrayList<>();

        // 先各自寻找到各自的孩子
        for (Menu menu : menus) {

            for (Menu e : menus) {
                if (menu.getId() == e.getParentId()) {
                    menu.getChildren().add(e);
                }
            }

            // 提取出父节点
            if (menu.getParentId() == 0L) {
                finalMenus.add(menu);
            }
        }

        return finalMenus;
    }
}
